package com.mcc.app.stock.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.mcc.app.cache.domain.PlcHisValueInfo;
import com.mcc.app.cache.mapper.PlcCacheMapper;
import com.mcc.app.cache.service.IPlcCacheService;
import com.mcc.app.process.domain.MixedAttribute;
import com.mcc.app.stock.domain.Silo;
import com.mcc.app.stock.mapper.SiloMapper;
import com.mcc.app.stock.service.ISiloService;
import com.mcc.common.core.text.Convert;
import com.mcc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//import com.mcc.app.process.service.IModeSinteringControlOutputService;

/**
 * 料仓Service业务层处理
 *
 * @author gxy
 * @date 2020-07-08
 */
@Service
public class SiloServiceImpl implements ISiloService {

    @Autowired
    private SiloMapper siloMapper;

    @Autowired
    private PlcCacheMapper plcMapper;


    @Autowired
    private IPlcCacheService plcCacheService;

//    @Autowired
//    private IModeSinteringControlOutputService modeSinteringControlOutputService;

    /**
     * 查询料仓
     *
     * @param siloId 料仓ID
     * @return 料仓
     */
    @Override
    public Silo selectSiloById(Long siloId) {
        Silo res = siloMapper.selectSiloById(siloId);
     //   getCacheDataBySilo(res);
        return res;
    }

    private void getCacheDataBySilo(Silo silo) {
        String weightCode = silo.getWeightCode();
        String cuttingCode = silo.getCuttingCode();
        String setCode = silo.getSetCode();
        String accuCode = silo.getAccudCode();  //需要修改
        String statusCode = silo.getStatusCode();
        if (StrUtil.isNotEmpty(weightCode)) {
             // 获取实时重量
            silo.setSiloWeight(getFormulaCalResultByCodeForPlus(weightCode));
            Float fullWeight = silo.getFullWeight();
            if (fullWeight != null && fullWeight > 0) {
                // 百分比计算
                silo.setSiloPercent(NumberUtil.mul(NumberUtil.div(silo.getSiloWeight(), fullWeight), 100).floatValue());
            }
        }
        if (StrUtil.isNotEmpty(cuttingCode)) {
            // 获取瞬时下料量
            silo.setSiloCutting(getFormulaCalResultByCodeForPlus(cuttingCode));
        }
        if (StrUtil.isNotEmpty(setCode)) {
            // 获取设定下料量
            silo.setCuttingSet(getFormulaCalResultByCodeForPlus(setCode));
            if (StrUtil.isNotEmpty(cuttingCode)) {
                List<String> statusList = StrUtil.split(statusCode, '|');
                List<String> setList = StrUtil.split(setCode, '+');
                if (setList.size() == statusList.size()) {
                    Float valueSum = 0F;
                    for (int i = 0; i < setList.size(); i++) {
                        String statusValue = plcMapper.getValueByCode(statusList.get(i));
                        String setValue = plcMapper.getValueByCode(setList.get(i));
                        if (StrUtil.isAllNotEmpty(statusValue, setValue)) {
                            if (!StrUtil.equals(statusValue, "0")) {
                                valueSum = NumberUtil.add(valueSum, Float.valueOf(setValue)).floatValue();
                            }
                        }
                    }
                    //silo.setEnableCuttingSet(valueSum);
                }
            }
        }
        if (StrUtil.isNotEmpty(accuCode)) {
            silo.setAccuWeight(getFormulaCalResultByCodeForPlus(accuCode));
        }

        if (StrUtil.isNotEmpty(statusCode)) {
            silo.setSiloStatus(getFormulaCalResultByCodeForOr(statusCode));
        }
    }

    private Float getFormulaCalResultByCodeForPlus(String code) {
        List<String> codeList = StrUtil.split(code, '+');
        Float valueSum = 0F;
        for (int i = 0; i < codeList.size(); i++) {
            String value = plcMapper.getValueByCode(codeList.get(i));
            if (StrUtil.isNotEmpty(value)) {
                valueSum = NumberUtil.add(valueSum, Float.valueOf(value)).floatValue();
            }
        }
        return valueSum;
    }

    private Integer getFormulaCalResultByCodeForOr(String code) {
        List<String> codeList = StrUtil.split(code, '|');
        Integer valueSum = 0;
        for (int i = 0; i < codeList.size(); i++) {
            String value = plcMapper.getValueByCode(codeList.get(i));
            if (StrUtil.isNotEmpty(value)) {
                if (!StrUtil.equals(value, "0")) {
                    valueSum |= 1;
                }
            }
        }
        return valueSum;
    }

    /**
     * 查询料仓列表
     *
     * @param silo 料仓
     * @return 料仓
     */
    @Override
    public List<Silo> selectSiloList(Silo silo) {
        List<Silo> siloList = siloMapper.selectSiloList(silo);
        for (Silo silo1:siloList){

            silo1.setMatterName(plcCacheService.getCacheDataToDouble(silo1.getMatterName()).toString());
            silo1.setStatusValue(Convert.toFloat(plcCacheService.getCacheDataToDouble(silo1.getStatusCode())));
            silo1.setWeightValue(silo1.getWeightCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getWeightCode())):0);
            silo1.setCuttingValue(silo1.getCuttingCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getCuttingCode())):0);
            silo1.setSetValue(silo1.getSetCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getSetCode())):0);
            silo1.setAccudValue(silo1.getAccudCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getAccudCode())):0);
            silo1.setAccunValue(silo1.getAccunCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getAccunCode())):0);
            silo1.setTotalValue(silo1.getTotalCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getTotalCode())):0);
            silo1.setPeibiValue(silo1.getPeibiCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPeibiCode())):0);
            silo1.setPianchaValue(silo1.getPianchaCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPianchaCode())):0);
            //silo1.setPreDayValue(silo1.getPreDayCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPreDayCode())):0);
            //silo1.setPreNightValue(silo1.getPreNightCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPreNightCode())):0);
            silo1.setPreAdayValue(silo1.getPreAdayCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPreAdayCode())):0);
            silo1.setSiloPercent(silo1.getPeibiCode()!=null? Convert.toFloat(plcCacheService.getRedisCache(silo1.getPeibiCode())):0);
        }
        return siloList;
    }

    /**
     * 新增料仓
     *
     * @param silo 料仓
     * @return 结果
     */
    @Override
    public int insertSilo(Silo silo) {
        return siloMapper.insertSilo(silo);
    }

    /**
     * 修改料仓
     *
     * @param silo 料仓
     * @return 结果
     */
    @Override
    public int updateSilo(Silo silo) {
        return siloMapper.updateSilo(silo);
    }

    /**
     * 批量删除料仓
     *
     * @param siloIds 需要删除的料仓ID
     * @return 结果
     */
    @Override
    public int deleteSiloByIds(Long[] siloIds) {
        return siloMapper.deleteSiloByIds(siloIds);
    }

    /**
     * 删除料仓信息
     *
     * @param siloId 料仓ID
     * @return 结果
     */
    @Override
    public int deleteSiloById(Long siloId) {
        return siloMapper.deleteSiloById(siloId);
    }

    @Override
    public int enableSilo(Silo silo) {
        return siloMapper.enableSilo(silo);
    }

    @Override
    public List<PlcHisValueInfo> getSiloDialog(Long siloId, String startDt, String endDt) {

        List<PlcHisValueInfo> plcHisValueInfos = new ArrayList<>();
        Silo silo = siloMapper.selectSiloById(siloId);
        /*PlcHisValueInfo plcHisValueInfo = plcHistoryInfo.getPlcHisValuesInfo(silo.getWeightCode(),startDt,endDt);
        PlcHisValueInfo plcHisValueInfo1 = plcHistoryInfo.getPlcHisValuesInfo(silo.getCuttingCode(),startDt,endDt);
        plcHisValueInfo.setPlcName("料槽料位");
        plcHisValueInfo1.setPlcName("瞬时下料量");
        plcHisValueInfos.add(plcHisValueInfo);
        plcHisValueInfos.add(plcHisValueInfo1);*/
        return plcHisValueInfos;
    }

    @Override
    public List<PlcHisValueInfo> mixingWaterVolume1Dialog(String startDt, String endDt) {

        List<PlcHisValueInfo> plcHisValueInfos = new ArrayList<>();
//        plcHisValueInfos.add(plcHistoryInfo.getPlcHisValuesInfo("CP_M_MAEM1_IQ",startDt,endDt));
        return plcHisValueInfos;
    }

    @Override
    public List<PlcHisValueInfo> mixingWaterVolume2Dialog(String startDt, String endDt) {

        List<PlcHisValueInfo> plcHisValueInfos = new ArrayList<>();
//        plcHisValueInfos.add(plcHistoryInfo.getPlcHisValuesInfo("CP_M_MAEM2_IQ",startDt,endDt));
        return plcHisValueInfos;
    }

    @Override
    public List<PlcHisValueInfo> totalMaterialQuantityDialog(String startDt, String endDt) {

        List<PlcHisValueInfo> plcHisValueInfos = new ArrayList<>();
//        plcHisValueInfos.add(plcHistoryInfo.getPlcHisValuesInfo("PL_M_MOA1_CUW",startDt,endDt));
        return plcHisValueInfos;
    }

    @Override
    public MixedAttribute getMixedAttrValue() {

        MixedAttribute mixedAttribute = new MixedAttribute();
        mixedAttribute.setMixingWaterVolume1(plcCacheService.selectPlcCacheByCode("st2_cp_m_awm1_iq").getCacheValue());
        mixedAttribute.setAddWaterVolume1(plcCacheService.selectPlcCacheByCode("st2_cp_m_awm2_iq").getCacheValue());
        mixedAttribute.setMixingWaterVolume2(plcCacheService.selectPlcCacheByCode("st2_cp_m_maem1_iq").getCacheValue());
        mixedAttribute.setAddWaterVolume2(plcCacheService.selectPlcCacheByCode("st2_cp_m_maem2_iq").getCacheValue());
        mixedAttribute.setTotalIngredientsAccum("100");
        mixedAttribute.setTotalIngredientsNow("300");
        mixedAttribute.setFurnacePressure(plcCacheService.selectPlcCacheByCode("st3_sj_c_pi2101").getCacheValue());
        mixedAttribute.setGasPressure(plcCacheService.selectPlcCacheByCode("st3_sj_c_pi2102").getCacheValue());
        mixedAttribute.setAirPressure(plcCacheService.selectPlcCacheByCode("st3_sj_c_pi2103").getCacheValue());
        mixedAttribute.setGasFlow(plcCacheService.selectPlcCacheByCode("st3_sj_c_fi2101").getCacheValue());
        mixedAttribute.setAirFlow(plcCacheService.selectPlcCacheByCode("st3_sj_c_fi2102").getCacheValue());
        mixedAttribute.setFireTemper1(plcCacheService.selectPlcCacheByCode("st3_sj_c_ti2103a").getCacheValue());
        mixedAttribute.setFireTemper2(plcCacheService.selectPlcCacheByCode("st3_sj_c_ti2103b").getCacheValue());
        mixedAttribute.setFireTemper3(plcCacheService.selectPlcCacheByCode("st3_sj_c_ti2103c").getCacheValue());
        mixedAttribute.setMaterialLayerHeight(averagePlcValue("st3_sj_c_li2301a,st3_sj_c_li2301b,st3_sj_c_li2301c,st3_sj_c_li2301d,st3_sj_c_li2301e"));
        mixedAttribute.setTrolleyFrequency(plcCacheService.selectPlcCacheByCode("st3_sj_c_4301_hz").getCacheValue());
        mixedAttribute.setMainPumpingFrequency(plcCacheService.selectPlcCacheByCode("st4_sj_c_9001bp_hz").getCacheValue());
        mixedAttribute.setDamperOpening(plcCacheService.selectPlcCacheByCode("st4_zc_c_hv4101z").getCacheValue());
        mixedAttribute.setAirVolume(plcCacheService.selectPlcCacheByCode("st4_zc_c_fiq4101").getCacheValue());
        mixedAttribute.setSmokeTemper(plcCacheService.selectPlcCacheByCode("st3_sj_c_ti2323").getCacheValue());
        mixedAttribute.setCoolerFrequency(plcCacheService.selectPlcCacheByCode("st5_sj_c_4501_hz").getCacheValue());
        mixedAttribute.setHighFlueGasTemper(plcCacheService.selectPlcCacheByCode("st5_sj_c_ti2203").getCacheValue());
        mixedAttribute.setLowFlueGasTemper(plcCacheService.selectPlcCacheByCode("st5_sj_c_ti2204").getCacheValue());
        mixedAttribute.setSinterMachineFrequency(plcCacheService.selectPlcCacheByCode("st3_sj_c_4301_hz").getCacheValue());
        mixedAttribute.setRoundRollFrequency(plcCacheService.selectPlcCacheByCode("st4_sj_c_4101_hz").getCacheValue());
        mixedAttribute.setManyRoundRollFrequency(plcCacheService.selectPlcCacheByCode("st4_sj_c_4201a_hz").getCacheValue());
//        mixedAttribute.setXBTP(String.valueOf(modeSinteringControlOutputService.newestOutput().getXBtp()));
        return mixedAttribute;
    }

    String averagePlcValue(String plcCodes){

        String[] plcCodeList= StringUtils.split(plcCodes,',');
        if (StringUtils.isEmpty(plcCodeList)){
            return "plcCodes为空";
        }
        double averValue = 0d;
        for (String plcCode:plcCodeList){
            averValue += Double.parseDouble(plcCacheService.selectPlcCacheByCode((plcCode)).getCacheValue());
        }
        return String.format("%.2f",averValue);

    }
}
